﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using B303Langf.Parser.AST;

namespace B303Langf.Parser
{
   public class IdentificationTable
   {
      private readonly List<Dictionary<string, Declaration>> m_Declarations;

      public IdentificationTable()
      {
         m_Declarations = new List<Dictionary<string, Declaration>>();
      }

      public int ScopeLevel
      {
         get
         {
            return m_Declarations.Count;
         }
      }

      public void Enter( string id, Declaration decl )
      {
         var dict = m_Declarations[ m_Declarations.Count - 1 ];
         dict.Add( id, decl );
      }

      public Declaration Retrieve( string id )
      {
         for( int i = m_Declarations.Count - 1 ; i > -1 ; i-- )
         {
            var dict = m_Declarations[ i ];
            Declaration output = null;
            if( dict.TryGetValue( id, out output ) )
            {
               return output;
            }
         }
         return null;
      }

      public void OpenScope()
      {
         m_Declarations.Add( new Dictionary<string, Declaration>() );
      }

      public void CloseScope()
      {
         m_Declarations.RemoveAt( m_Declarations.Count - 1 );
      }
   }
}
